package com.obtk.controllerSGW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.modelSGW.CstServer;
import com.obtk.service.CustomerServerService;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/cstServer")
public class CustomerServerController {

    @Autowired
    private CustomerServerService customerServerService;

    @PostMapping("/list")
    @ResponseBody
    public PageResultVo getCstServer(CstServer cstServer, Integer limit, Integer offset,Model model) {
        PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
//        System.out.println(cstServer);
        List<CstServer> cstServerList = customerServerService.getCstServer(cstServer);
        PageInfo<CstServer> pages = new PageInfo<>(cstServerList);
        model.addAttribute("abc","ashahahahafasdsf");
        return ResultUtil.table(cstServerList, pages.getTotal());
    }
    //添加服务
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo addCstServer(CstServer cstServer, Integer limit, Integer offset) {
        System.out.println(cstServer);
        int num = customerServerService.addCstServer(cstServer);
        if (num > 0) {
            return ResultUtil.success("添加服务成功");
        } else {
            return ResultUtil.error("添加服务失败");
        }
    }
    //服务分配
    @GetMapping("/distribute")
    @ResponseBody
    public ResponseVo distribute(CstServer cstServer) {
        String[] svrIdsList = cstServer.getSvrId().split(",");
        int num = customerServerService.distribute(cstServer, svrIdsList);
        if (num > 0) {
            return ResultUtil.success("分配成功");
        } else {
            return ResultUtil.error("分配失败");
        }
    }
    //服务处理
    @GetMapping("/dealWith")
    @ResponseBody
    public ResponseVo dealWith(CstServer cstServer) {
        String[] svrIdsList = cstServer.getSvrId().split(",");
        int num = customerServerService.dealWith(cstServer, svrIdsList);
        if (num > 0) {
            return ResultUtil.success("执行成功");
        } else {
            return ResultUtil.error("执行失败");
        }
    }
    //更改状态
    @GetMapping("/changeStatus")
    @ResponseBody
    public ResponseVo changeStatus(CstServer cstServer) {
        String[] svrIdsList = cstServer.getSvrId().split(",");
        System.out.println(cstServer);
        int num = customerServerService.changeStatus(cstServer, svrIdsList);
        if (num > 0) {
            return ResultUtil.success("更改成功");
        } else {
            return ResultUtil.error("更改失败");
        }
    }
    //服务归档
    @PostMapping("/placeOnFile")
    @ResponseBody
    public ResponseVo placeOnFile(CstServer cstServer) {
        String[] svrIdsList = cstServer.getSvrId().split(",");
        int num = customerServerService.placeOnFile(cstServer, svrIdsList);
        if (num > 0) {
            return ResultUtil.success("执行成功");
        } else {
            return ResultUtil.error("执行错误");
        }
    }

    @PostMapping("/abc")
    public String abc(Model model) {
       model.addAttribute("abc","asdfasdsf");
       return "/mg-fw/list";
    }
}